<template>
  <div class="table-box">
    <div class="card table-main">
      <el-table :data="tableData" style="width: 100%" border>
        <el-table-column prop="message" label="描述" align="center" />
        <el-table-column prop="resultType" label="结果类型" width="200" align="center" />
        <el-table-column prop="endTime" label="结束时间" width="200" align="center" />
        <el-table-column label="操作" width="140" align="center">
          <template #default="scope">
            <el-button type="primary" size="small" :icon="Download" @click="downloadExcel(scope.row)">下载表格</el-button>
          </template>
        </el-table-column>
        <template #empty>
          <div class="table-empty">
            <slot name="empty">
              <img src="@/assets/images/notData.png" alt="notData" />
              <div>暂无数据</div>
            </slot>
          </div>
        </template>
      </el-table>

      <el-pagination
        background
        layout="prev, pager, next, total"
        :total="total"
        v-model:current-page="query.page"
        v-model:page-size="query.size"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts" name="browseLog">
import { ref } from "vue";
import { Download } from "@element-plus/icons-vue";
import { downloadBrowseResultApi, getBrowseResultApi } from "@/api/modules/business";
import { downDocument } from "@/utils";

const total = ref(0);
const query = ref({ page: 1, size: 10 });
const tableData = ref<Record<string, any>[]>([]);

async function downloadExcel(row: Record<string, any>) {
  const data = await downloadBrowseResultApi(row.resultId);
  downDocument(data, "浏览结果.xlsx");
}

function handleSizeChange(num: number) {
  query.value.page = 1;
  query.value.size = num;
  getResult();
}

function handleCurrentChange(num: number) {
  query.value.page = num;
  getResult();
}

async function getResult() {
  const data = await getBrowseResultApi(query.value.page, query.value.size);
  tableData.value = data.resultList || [];
  total.value = data.total || 0;
}

getResult();
</script>
